package com.vibratetoggle;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.RemoteViews;

public class VibrateToggleWidget extends AppWidgetProvider {
	
	private static final ComponentName THIS_WIDGET = new ComponentName("com.vibratetoggle", "com.vibratetoggle.VibrateToggleWidget");
	
	public static final String TOGGLE_VIBRATE = "com.vibratetoggle.action.TOGGLE_VIBRATE";
	public static final String TAG = "VibrateToggle";
	public static final int VIBRATE_OFF = 0;
	public static final int VIBRATE_ON = 1;
	public static final int VIBRATE_MIDDLE = 2;
	
	private CustomLog log = new CustomLog();
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		
		log.v(TAG, "onUpdate");
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		
		Intent intent = new Intent(context, VibrateToggleWidget.class);
		intent.setAction(TOGGLE_VIBRATE);		
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
		
		views.setOnClickPendingIntent(R.id.vibrateToggle, pendingIntent);
		appWidgetManager.updateAppWidget(appWidgetIds, views);
		
		updateWidget(context);
		
	}
	
	
	@Override
	public void onReceive(Context context, Intent intent) {
		
		log.v(TAG, "onReceive");
		log.v(TAG, "intent: " + intent.getAction());
		
		if(intent.getAction().equals(TOGGLE_VIBRATE)) {
			log.v(TAG, "TOGGLE_VIBRATE action received");
			
			modifyRingerMode(context);
		}
		else {
			updateWidget(context);
		}
		
		super.onReceive(context, intent);
	}
	
	
	private void updateWidget(Context context) {
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
		
		switch(currentRingerState(context)) {
			case VIBRATE_ON:
				views.setImageViewResource(R.id.bar, R.drawable.bar_on);
				break;
				
			case VIBRATE_OFF:
				views.setImageViewResource(R.id.bar, R.drawable.bar_off);
				break;
				
			case VIBRATE_MIDDLE:
				views.setImageViewResource(R.id.bar, R.drawable.bar_middle);
				break;
		}
		
		AppWidgetManager.getInstance(context).updateAppWidget(THIS_WIDGET, views);
	}
	
	
	private int currentRingerState(final Context context) {
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		
		int vibrateRinger = VIBRATE_OFF;
		int vibrateNotification = VIBRATE_OFF;
		int ringerMode = audioManager.getRingerMode(); 
		
		log.v(TAG, "onUpdate");
		
		switch(audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER)) {
			case AudioManager.VIBRATE_SETTING_OFF:
				log.v(TAG, "Vibrate ringer off");
				vibrateRinger = VIBRATE_OFF;
				break;
				
			case AudioManager.VIBRATE_SETTING_ON:
				log.v(TAG, "Vibrate ringer on");
				vibrateRinger = VIBRATE_ON;
				break;
				
			case AudioManager.VIBRATE_SETTING_ONLY_SILENT:
				log.v(TAG, "Vibrate ringer only silent");
				vibrateRinger = VIBRATE_MIDDLE;
				break;
		}
		
		switch(audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
			case AudioManager.VIBRATE_SETTING_OFF:
				log.v(TAG, "Notification ringer off");
				vibrateNotification = VIBRATE_OFF;
				break;
				
			case AudioManager.VIBRATE_SETTING_ON:
				log.v(TAG, "Notification ringer on");
				vibrateNotification = VIBRATE_ON;
				break;
				
			case AudioManager.VIBRATE_SETTING_ONLY_SILENT:
				log.v(TAG, "Notification ringer only silent");
				vibrateNotification = VIBRATE_MIDDLE;
				break;
		}
		
		if(vibrateRinger == VIBRATE_ON && vibrateNotification == VIBRATE_ON) {
			if(audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
				log.v(TAG, "CurrentRingerState(): (ON,ON,RINGER_MODE_VIBRATE) => ret VIBRATE_MIDDLE");
				return VIBRATE_MIDDLE;
			}
			else {
				log.v(TAG, "CurrentRingerState(): (ON,ON,!RINGER_MODE_VIBRATE) => ret VIBRATE_ON");
				return VIBRATE_ON;
			}
		}
		else if(vibrateRinger == VIBRATE_OFF && vibrateNotification == VIBRATE_OFF) {
			log.v(TAG, "CurrentRingerState(): (OFF,OFF) => ret VIBRATE_OFF");
			return VIBRATE_OFF;
		}
		else if(vibrateRinger == VIBRATE_MIDDLE && vibrateNotification == VIBRATE_MIDDLE && ringerMode != AudioManager.RINGER_MODE_VIBRATE) {
			log.v(TAG, "CurrentRingerState(): ret (MIDDLE,MIDDLE) => VIBRATE_OFF");
			return VIBRATE_OFF;
		}
		else {
			log.v(TAG, "CurrentRingerState(): ret VIBRATE_MIDDLE");
			return VIBRATE_MIDDLE;
		}
	}
	
	
	private void modifyRingerMode(Context context) {
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		
		switch(currentRingerState(context)) {
			case VIBRATE_OFF:
				log.v(TAG, "modifyRingerMode(): set vibration ON");
				audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON);
				audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
				break;
				
			case VIBRATE_ON:
				log.v(TAG, "modifyRingerMode(): set vibration OFF");
				audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
				audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF);
				break;
				
			case VIBRATE_MIDDLE:
				log.v(TAG, "modifyRingerMode(): don't set vibration");
				break;
		}
	}
}
